﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;

namespace PMS.Web
{
    public partial class AddProject : System.Web.UI.Page
    {
        PMS_ProjectBLL bll = new PMS_ProjectBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string roleID = Request["roleID"];
                string id = Request["id"];
                if (!string.IsNullOrEmpty(id) && id != "0")
                {
                    if (!string.IsNullOrEmpty(Request["t"]) && Request["t"] == "1")
                    {
                        btn_Create.Visible = false;
                    }
                    PMS_Project model = bll.GetModel(Convert.ToInt32(id));
                    if (model != null)
                    {
                        hid_RoleID.Value = model.roleID.ToString();
                        hid_ID.Value = id;
                        lab_City.Text = model.city;
                        txt_ProjectID.Text = model.projectID;
                        txt_ProjectID.Enabled = false;
                        txt_Construction.Text = model.construction;
                        txt_Customer.Text = model.customer;
                        txt_CustomerTel.Text = model.customerTel;
                        txt_Design.Text = model.design;
                        txt_Engineer.Text = model.engineer;
                        txt_EngineerTel.Text = model.engineerTel;
                        txt_JDEID.Text = model.JDEID;
                        txt_Principal.Text = model.principal;
                        txt_ProjectName.Text = model.projectName;
                        txt_Sale.Text = model.sale;
                        txt_SystemPath.Text = model.systemPath;
                        if (!string.IsNullOrEmpty(model.systemProperty))
                        {
                            string[] listS = model.systemProperty.Split('|');
                            foreach (string li in listS)
                            {
                                if (string.IsNullOrEmpty(li))
                                    continue;
                                if (cbl_SystemProperty.Items.FindByText(li) != null)
                                    cbl_SystemProperty.Items.FindByText(li).Selected = true;
                                else
                                    txt_SystemProperty.Text = li;
                            }

                        }

                        if (ddl_ProjectType.Items.FindByText(model.projectType) != null)
                        {
                            ddl_ProjectType.Items.FindByText(model.projectType).Selected = true;
                        }

                        if (cbl_FMASurveyPoints.Items.FindByText(model.FMASurveyPoints) != null)
                        {
                            cbl_FMASurveyPoints.Items.FindByText(model.FMASurveyPoints).Selected = true;
                        }
                        if (cbl_Provider.Items.FindByText(model.provider) != null)
                        {
                            cbl_Provider.Items.FindByText(model.provider).Selected = true;
                        }

                        txt_StartDate.Value = model.startDate.ToString("yyyy-MM-dd");
                        txt_EndDate.Value = model.endDate.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>closeWin();</script>");
                    }
                }
                else if (!string.IsNullOrEmpty(roleID) && roleID != "0")
                {
                    BLL.PMS_RoleListBLL rbll = new BLL.PMS_RoleListBLL();
                    PMS_RoleList model = rbll.GetModel(Convert.ToInt32(roleID));
                    string[] roleIDs = model.roleIDs.Split('|');
                    model = rbll.GetModel(Convert.ToInt32(roleIDs[1]));
                    lab_City.Text = model.roleText;
                    hid_RoleID.Value = roleID;
                }
                else
                {
                    this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>closeWin();</script>");
                }
            }
        }

        protected void btn_Create_Click(object sender, EventArgs e)
        {
            PMS_Project project = new PMS_Project();
            project.city = lab_City.Text;
            project.construction = txt_Construction.Text;
            project.customer = txt_Customer.Text;
            project.customerTel = txt_CustomerTel.Text;
            project.design = txt_Design.Text;
            if (txt_EndDate.Value != "")
            {
                project.endDate = Convert.ToDateTime(txt_EndDate.Value);
            }
            if (txt_StartDate.Value != "")
            {
                project.startDate = Convert.ToDateTime(txt_StartDate.Value);
            }

            project.engineer = txt_Engineer.Text;
            project.engineerTel = txt_EngineerTel.Text;
            project.FMASurveyPoints = cbl_FMASurveyPoints.Text;
            project.JDEID = txt_JDEID.Text;
            project.principal = txt_Principal.Text;
            project.projectID = txt_ProjectID.Text;
            project.projectName = txt_ProjectName.Text;
            project.provider = cbl_Provider.Text;
            project.roleID = Convert.ToInt32(hid_RoleID.Value);
            project.sale = txt_Sale.Text;
            project.systemPath = txt_SystemPath.Text;

            string systemProperty = "";
            foreach (ListItem li in cbl_SystemProperty.Items)
            {
                if (li.Selected)
                {
                    systemProperty += li.Text + "|";
                    if (li.Text == "其它")
                        systemProperty += txt_SystemProperty.Text;
                }
            }
            project.systemProperty = systemProperty;

            project.projectType = ddl_ProjectType.Text;
            project.id = Convert.ToInt32(hid_ID.Value);

            if (project.id == 0 && bll.GetModel(project.projectID,project.projectType) != null)
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('项目编号已存在');</script>");
                return;
            }
            PMS_User user = PageComm.GetUser(this.Response);
            project.createName = user.id.ToString();
            if (bll.AddUpdate(project))
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('保存成功');closeWin();</script>");
            }
        }
    }
}